<!DOCTYPE html>
<!--
This is a starter template page. Use this page to start your new project from
scratch. This page gets rid of all links and provides the needed markup only.
-->
<html>
<head th:replace="common/head :: common_head(~{::title},~{})">
  <title th:text="'财税风险分析系统 | '+${report.reportSpec.name}"></title>
</head>
<style>
	.cetr{
		text-align: center;
	}
</style>

<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <div th:replace="common/header-user :: header"></div>
  
  <div th:replace="common/aside-user :: aside"></div>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
  
    <section class="content-header">
      <h1 id="page-title" active-aside="analysis-aside" active-menu="enterprise-income-tax-return-menu">企业所得税纳税申报表
        <small th:if="${#strings.equalsIgnoreCase(report.reportGroup.period,'ANNUAL')}">[[${#temporals.year(report.reportGroup.startDate)}]]年度</small>
        <small th:if="${#strings.equalsIgnoreCase(report.reportGroup.period,'SEMIANNUAL')}">[[${#temporals.month(report.reportGroup.startDate)&eq;1?'上':'下'}]半年度</small>
        <small th:if="${#strings.equalsIgnoreCase(report.reportGroup.period,'QUARTERLY')}">[[${#temporals.month(report.reportGroup.startDate)}]]季度</small>
        <small th:if="${#strings.equalsIgnoreCase(report.reportGroup.period,'MONTHLY')}">[[${#temporals.month(report.reportGroup.startDate)}]]月度</small>
      </h1>
      <ol class="breadcrumb">
        <li><a th:href="'/client/report/tax/index/'+${report.reportGroup.enterprise.id}"><i class="fa fa-list"></i> 企业所得税纳税申报表</a></li>
        <li><a th:href="'/client/report/tax/list/'+${report.reportGroup.id}">列表</a></li>
        <li class="active" th:text="${report.reportSpec.name}"></li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content container-fluid">
      <div class="row">
        <div class="col-sm-12">
          <div class="box">
            <div class="box-header with-border">
              <h3 class="box-title" th:text="${report.reportSpec.name}">资产负债表<small></small></h3>
              <h3 class="box-title pull-right"><small><!-- 单位：元 --></small></h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body" id="report-table-div">
            
            </div>
            <!-- /.box-body -->
            <div class="box-footer clearfix">
              <button type="button" class="btn btn-block btn-success">保存</button>
            </div>
          </div>
          <!-- /.box -->
        </div>
      </div>
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  
  <div th:replace="common/footer :: footer"></div>

  <!-- Add the sidebar's background. This div must be placed
  immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->

<div th:replace="common/script :: script"></div>

<script th:inline="javascript">
  function amount(obj){
    obj.value = obj.value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符  
    obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的  
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$","."); 
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数  
    if(obj.value.indexOf(".")< 0 && obj.value !=""){//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额 
        obj.value= parseFloat(obj.value); 
    }  
    //obj.value = new Number(obj.value).toFixed(2);
  }
  
  function genReportTable() {
	  var rowSpecs = [[${report.reportSpec.template.rows}]];
    var columnSpecs = [[${report.reportSpec.template.columns}]];
    
	  var table_html = '<table class="table table-bordered">';
	  for(i = 0; i < rowSpecs.length + 3; i++) {
		  table_html += '<tr>'
		  for(j = 0; j < columnSpecs.length - 4; j++) {
			  if(i == 0) {
				  table_html += '<th class="rowIndex' + i + ' columnIndex' + j + ' cetr"></th>';
			  } else {
				  if(i > 1 && j >= 2 && j < columnSpecs.length) {
					  table_html += '<td class="rowIndex' + i + ' columnIndex' + j + '">'
	                + '<input type="number" step="0.01" rowIndex="' + i + '" columnIndex="' + j + '"  class="form-control input-td inputValue" placeholder="0.00">'
	                + '</td>';
				  } else {
					  table_html += '<td class="rowIndex' + i + ' columnIndex' + j + '"></td>';
				  }
			  }
		  }
		  table_html += '</tr>';
	  }
	  table_html += '</table>';
	  
	  $('#report-table-div').html(table_html);
	  
	  showTemplate();
  }
  
  function showTemplate() {
	  var rowSpecs = [[${report.reportSpec.template.rows}]];
	  var columnSpecs = [[${report.reportSpec.template.columns}]];
	  var cells = [[${report.cells}]];
	  
	  if(rowSpecs && rowSpecs.length>0) {
		  $.each(rowSpecs, function(i, v) {
			  var _rowIndex = v.rowIndex;
			  var _columnIndex = v.columnIndex;
			  $(".rowIndex" + _rowIndex + ".columnIndex" + _columnIndex).text(v.name);
			  if(v.number){
          $(".rowIndex" + _rowIndex + ".columnIndex" + (parseInt(_columnIndex) - 1)).text(v.number).css("text-align", "center");
        }
			  if(v.span > 0) {
				  $(".rowIndex" + _rowIndex + ".columnIndex" + _columnIndex).attr("rowspan", v.span).css("vertical-align", "middle");
				  for(i = Number(_rowIndex) + 1; i < Number(_rowIndex) + Number(v.span); i++) {
					  $(".rowIndex" + i + ".columnIndex" + _columnIndex).remove();
				  }
			  }
		  });
	  }
	  
	  if(columnSpecs && columnSpecs.length > 0) {
		  $.each(columnSpecs, function(i, v){
			  var _rowIndex = v.rowIndex;
			  var _columnIndex = v.columnIndex;
			  $(".rowIndex" + _rowIndex + ".columnIndex" + _columnIndex).text(v.name);
			  if(v.number){
				  if(i == 2 || i == 3) {
					  $(".rowIndex" + (parseInt(_rowIndex) + 2) + ".columnIndex" + _columnIndex).text(v.number).css("text-align", "center");
				  } else {
					  $(".rowIndex" + (parseInt(_rowIndex) + 1) + ".columnIndex" + _columnIndex).text(v.number).css("text-align", "center");
				  }
			  }
			  if(v.span > 0) {
          $(".rowIndex" + _rowIndex + ".columnIndex" + _columnIndex).attr("rowspan", v.span).css("vertical-align", "middle");
          for(i = Number(_rowIndex) + 1; i < Number(_rowIndex) + Number(v.span); i++) {
            $(".rowIndex" + i + ".columnIndex" + _columnIndex).remove();
          }
        } else if(v.span < 0) {
          $(".rowIndex" + _rowIndex + ".columnIndex" + _columnIndex).attr("colspan", 0 - v.span).css("text-align", "center");
          for(i = Number(_columnIndex) + 1; i < Number(_columnIndex) + (0 - Number(v.span)); i++) {
            $(".rowIndex" + _rowIndex + ".columnIndex" + i).remove();
          }
        }
		  });
	  }
	  
	  if(cells && cells.length > 0){
		  $.each(cells,function(i, v){
			  var _rowIndex = v.rowIndex;
			  var _columnIndex = v.columnIndex;
			  var _val = v.value;
			  $(".rowIndex" + _rowIndex + ".columnIndex" + _columnIndex + " .inputValue").val(_val);
			  $(".rowIndex" + _rowIndex + ".columnIndex" + _columnIndex + " .inputValue").attr("cellid", v.id);
		  });
	  }
  }
  
  $(function(){
	  genReportTable();
	  
	  $(".btn-success").click(function() {
		  var $inputValues = $(".inputValue");
		  if($inputValues && $inputValues.length > 0) {
			  var params = [];
			  $inputValues.each(function(i, v) {
				  console.log(v, v.getAttribute("rowIndex"));
				  var _rowIndex = v.getAttribute("rowIndex");
				  var _columnIndex = v.getAttribute("columnIndex");
				  var _val = v.value;
				  var _id = v.getAttribute("cellid");
				  if(!_val){
					  _val = '';
				  }
				  params.push('{"id":"'+_id+'","value":"'+_val+'","rowIndex":"'+_rowIndex+'","columnIndex":"'+_columnIndex+'"}');
			  });
			  var _id = [[${report.id}]];
			  $.ajax({
				  url:'/client/report/addReport/' + _id,
				  dataType:'json',
				  contentType:'application/json;charset=utf-8',
				  data:params.join(","),
				  type:'post',
				  success:function(res){
					  location.href="/client/report/tax/list/" + res;
				  }
			  })
			  
		  }
	  })
  })
</script>

</body>
</html>